﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NhanAi.Entity;
using NhanAi.DAO;
using NhanAi.DAO.Mix;
using Home.Utils;
using NhanAi.App_Code;
namespace NhanAi.Admin
{
    public partial class EditQA : BasePage
    {
        int id;
        public int Id { get { return id; } }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Authenticate();
            if (!IsPostBack)
            {
                int id = TypeWrapper.GetInt32(Request["id"]);
                PostModel daoPost = new PostModel();
                TaxonomyDao daoTax = new TaxonomyDao();
                Post p = daoPost.Read(id, NAConstants.ALL_LANGUAGE);
                hdnId.Value = id.ToString();
                txtTitle.Text = p.Title;
                id = p.Id;
                if (!IsPostBack)
                {
                    PostContent.Value = p.Content;
                    PostContentEn.Value = p.ContentEn;

                    cmbStatus.Items.Add(new ListItem("Nháp", NAConstants.POST_STA_DRAFT.ToString()));
                    cmbStatus.Items.Add(new ListItem("Cho phép đăng", NAConstants.POST_STA_POST.ToString()));

                    int index = 0;
                    foreach (ListItem item in cmbStatus.Items)
                    {
                        if (item.Value == p.Status.ToString())
                        {
                            cmbStatus.SelectedIndex = index;
                        }
                        index++;
                    }
                }
            }
            if(Request.HttpMethod=="POST")
            {
                bool rs = false;
                if (!String.IsNullOrEmpty(Request["delete"]))
                {
                    int id = TypeWrapper.GetInt32(Request["Id"]);
                    PostDao dao = new PostDao();
                    try
                    {
                        dao.Delele(id);
                        rs = true;
                    }
                    catch (Exception ex) { SetMessage("Đã xảy ra lỗi",""); }
                    
                }
                if(rs)Redirect("/Admin/ListQA.aspx");
            }

            SetCurrentPage("Post");
        }
        public void btnUpdateClick(object sender, EventArgs e)
        {
            bool rs = false;
            try
            {
                PostDao daoPost = new PostDao();
               
                int id = TypeWrapper.GetInt32(hdnId.Value);
                Post p = daoPost.Read(id, NAConstants.ALL_LANGUAGE);

                p.Content = PostContent.Value;
                p.ContentEn = PostContentEn.Value;
                p.Status = int.Parse(cmbStatus.SelectedItem.Value);
               
                p.ModifiedDate = DateTime.Now;             
           
                daoPost.Update(p);
                rs = true;
            }
            catch (Exception ex)
            {
                SetMessage("Đã xảy ra lỗi","");
            }
            if(rs)SetMessage("Cập nhật thành công", "");
        }
      
        protected void btnReset_Click(object sender, EventArgs e)
        {
           
        }
    }
}